package study.stream.groupBy;

import study.User;
import study.Utils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author 金贵权
 * @Date 2023/6/16 10:00
 **/
public class Demo01 {
    public static void main(String[] args) {
        List<User> userList = Utils.getUserList();
//        Map<String, Integer> collect = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.summingInt(User::getId)));
        LinkedHashMap<String, LinkedHashMap<Integer, List<User>>> collect = userList.stream().collect(Collectors.groupingBy(
                User::getName, LinkedHashMap::new,
                Collectors.groupingBy(User::getId, LinkedHashMap::new, Collectors.toList())));
        Iterator<String> iterator = collect.keySet().iterator();
        List<UserName> userNameList = new ArrayList<>();
        while (iterator.hasNext()){
            String name = iterator.next();
            UserName userName = new UserName();
            userName.setName(name);
            LinkedHashMap<Integer, List<User>> integerListLinkedHashMap = collect.get(name);
            Iterator<Integer> iterator1 = integerListLinkedHashMap.keySet().iterator();
            List<UserId> userIdList = new ArrayList<>();
            while (iterator1.hasNext()){
                Integer id = iterator1.next();
                UserId userId = new UserId();
                userId.setId(id);
                List<User> list = integerListLinkedHashMap.get(id);
                userId.setUserList(list);
                userIdList.add(userId);
            }
            userName.setUserIdList(userIdList);
            userNameList.add(userName);
        }
        System.out.println(userNameList);

    }
}
